<template>
    <orh-table reload  @on-reload="reload" add="add" pages @on-add="displays" del="del" @on-del="confirm" :pageDate="pageDate">
        <Table :columns="columns1" :data="SchoolYearList" slot="table" @on-selection-change="selectOne" :loading="tableload"  highlight-row ref="currentRowTable"></Table>
        <orh-sidebar v-model="show" :title="layouttitle" scale slot="others">
            <component :is="content" :infoId='infoId' @requery="requery"></component>
        </orh-sidebar>
    </orh-table>
</template>

<script>
export default {
    components: {
        Operation: resolve => {require(['./Operation.vue'], resolve);},
    },
    name: "basicmodule-schoolyear",
    layout:'menu',
      data () {
            return {
                content:'',
                pageDate: {
                    total: 0,
                    current_page: 1,
                    per_page: 1
                },
		        del:[],
                show: false,
                layouttitle:null,
                delUrl:'api/semester/v1/schoolyear/delete/',
                infoId: null,
                tableload:true,
                SchoolYearList:[],
                columns1: [
                    {
                        type: 'selection',
                        width: 80,
                        align: 'center',
                    },
                    {
                        title: '学校名称',                             
                        render: (h, params) => {
                            return h('div', params.row.school.school_name.zh?params.row.school.school_name.zh:params.row.school.school_name.mn);
                        }
                    },
                    {
                        title: '学年名称',                   
                         render: (h, params) => {
                            return h('div', params.row.schoolyear_name.zh?params.row.schoolyear_name.zh:params.row.schoolyear_name.mn);
                        }
                    },
                    {
                        title: '开始时间',
                        key: 'start_at',
                    },
                    {
                        title: '结束时间',
                        key: 'end_at'
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    directives: [
                                        {
                                            name: 'button',
                                            value: 'edit',
                                        }
                                    ],
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px',
                                    },
                                    on: {
                                        click: () => {
                                            this.update(params.row.schoolyear_id)
                                        }
                                    }
                                }, '编辑'),
                                h('Button', {
                                    directives: [
                                        {
                                            name: "button",
                                            value: "del"
                                        }
                                    ],
                                    props: {
                                        type: 'error',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px',
                                    },
                                    on: {
                                        click: () => {
                                            this.del = [params.row.schoolyear_id]
                                            this.confirm()
                                        }
                                    }
                                }, '删除'),
                            ]);
                        }
                    }
                ],
            }
        },
        
        methods: {           
            // 获取学年的数据
            SchoolYear(index){
                this.tableload = true
                this.$http.get(`api/semester/v1/schoolyear/lists?page=${index}`).then(val=>{
                    this.SchoolYearList=val.data
                    this.pageDate.total=val.pages.total
                    this.pageDate.per_page=val.pages.per_page
                    this.pageDate.current_page=val.pages.current_page
                    this.tableload = false 
                }).catch(err => {
                    if (err.code && err.code == 422) {
                        this.$Message.error(err.message);
                    }
                    this.tableload = false
                })
            }, 
            // 点击编辑时向子组件传数据
            update(index){
                this.infoId = index
                this.show=true
			    this.content = 'Operation' 
                this.layouttitle='修改'
            },
            /************************************************************
             * 批量删除
             ************************************************************/
            selectOne(index){
                this.del = index.map(item=>item.schoolyear_id)
            },
            /************************************************************
             * 执行删除操作
             ************************************************************/
            confirm () {
                this.$Modal.confirm({
                    title: '数据删除',
                    content: '<p>您确定要删除这些数据吗?</p>',
                    onOk: () => {
                        if(this.del.length==0){
                            this.$Message.error('请选择删除项');
                            return
                        }
                        this.del = JSON.stringify(this.del)
                        this.tableload = true 
                        this.$http.delete(`${this.delUrl}${this.del}`).then(val=>{  
                            this.del=[]
                            this.reload(this.pageDate.current_page)    
                            this.$Message.success(val.message);           
                        }).catch(err=>{
                            if(err.code && err.code==422) {
                                this.$Message.error(err.message);
                            }
                            this.del=[]
                            this.tableload = false 
                        })
                    },
                });
            },
            // 修改返回数据更新
            requery(val){            
                if(val == true){
                    this.show = false
                    this.reload(this.pageDate.current_page);
                }
            },
            reload (index) {
                this.SchoolYearList = []
                this.SchoolYear (index?index:1)
            },
            //添加按钮
            displays(){
                this.infoId = null
                this.show=true
			    this.content = 'Operation' 
                this.layouttitle='添加'
            },
        },
        mounted(){
            this.reload ()
        },
}
</script>
